package com.lobo.blog.controller;

import com.github.pagehelper.PageInfo;
import com.lobo.blog.entity.Blog;
import com.lobo.blog.entity.Tag;
import com.lobo.blog.entity.Type;
import com.lobo.blog.service.BlogService;
import com.lobo.blog.service.TagService;
import com.lobo.blog.service.TypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.List;

/**
 * @authoer:MacieSerenity
 * @createDate: 2022/10/15
 * @description:
 */
@Controller
public class TagsController {
    @Resource
    TagService tagService;
    @Resource
    BlogService blogService;

    @RequestMapping(value = "/tags/{id}", method = RequestMethod.GET)
    public String toTypesInfo(@PathVariable Integer id,
                              Integer page,
                              Model model) {
        List<Tag> topTagBySize = tagService.getTopTagBySize(100);
        if (id==-1) {id=topTagBySize.get(0).getId();}
        model.addAttribute("tags",topTagBySize);
        if (page==null){
            page=1;
        }
        PageInfo<Blog> blogsByTag = blogService.getBlogsByTag(page, id);
        model.addAttribute("page",blogsByTag);
        model.addAttribute("activeTagId",id);
        return "tags";
    }

}
